# Probability of winning an attack (Attack dice, Defence Dice)
# [0] chance of Attacker winning
# [1] chance of a Split
# [2] chance of Defender winning
threetwo=[0.37165637860082307, 0.33577674897119342, 0.29256687242798352]
threeone= [0.65972222222222221, 0.0, 0.34027777777777779]
twotwo=[0.22762345679012347, 0.32407407407407407, 0.44830246913580246]
twoone=[0.57870370370370372, 0.0, 0.42129629629629628]
onetwo=[0.25462962962962965, 0.0, 0.74537037037037035]
oneone=[0.41666666666666669, 0.0, 0.58333333333333337]

# merges if for merging two list of topals and scales the probability in the list.
def merges(prob,lista,listb):
    result= []
    a = 0
    b = 0
    while a<len(lista) and b<len(listb):
        (oddsa,armya)=lista[a]
        (oddsb,armyb)=listb[b]
        if armya==armyb:
            result.append((prob*oddsa+(1-prob)*oddsb,armya))
            a=a+1
            b=b+1
        elif armya> armyb:
            result.append((prob*oddsa,armya))
            a=a+1
        elif armyb> armya:
            result.append(((1-prob)*oddsb,armyb))
            b=b+1
    while a<len(lista):
        (oddsa,armya)=lista[a]
        result.append((prob*oddsa,armya))
        a=a+1
    while b<len(listb):
        (oddsb,armyb)=listb[b]
        result.append(((1-prob)*oddsb,armyb))
        b=b+1
    return result

# This is the code to make a 30 by 30 list and fills it with 0
maxtotal=31
odds=[]
for i in range (maxtotal):
    row= []
    for j in range (maxtotal):
        row.append(0)
    odds.append(row)
# This code goes through the 2d list diagonaly and finds the probibility of each attack and defence armies winning
for sum in range (1,maxtotal):
    #a and d refer to the number of attacking and defending armies
    for a in range(0,sum+1):
        d=sum-a
        print "odds["+str(a)+"]["+str(d)+"]"
        if a==0:
            odds[a][d]=[(1,-d)]
        elif d==0:
            odds[a][d]=[(1,a)]
        elif a==1 and d==1:
            odds[a][d]=[(oneone[0],1),(oneone[2],-1)]
        elif a==2 and d==1:
            odds[a][d]=merges(twoone[0], odds[2][0], odds[1][1])
        elif a>=3 and d==1:
            odds[a][d]=merges(threeone[0],odds[a][0], odds[a-1][1])
        elif a==1 and d>=2:
            odds[a][d]= merges(onetwo[0],odds[1][d-1],odds[0][d])
        elif a==2 and d>=2:
            odds[a][d]=merges(twotwo[0],odds[2][d-2],merges(twotwo[1]/(twotwo[1]+twotwo[2]),odds[1][d-1],odds[0][d]))
        elif a>=3 and d>=2:
            odds[a][d]=merges(threetwo[0],odds[a][d-2],merges(threetwo[1]/(threetwo[1]+threetwo[2]),odds[a-1][d-1],odds[a-2][d]))
        print "odds["+str(a)+"]["+str(d)+"] = "+ str(odds[a][d])